package com.ect.server.test;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

import com.ect.model.MessageEntity;
/**
 * 
 * @author zhouxiaochen 2011-11-14
 *
 */
public class Server2 {
	public Server2() {
		try {
			ServerSocket ss = new ServerSocket(10001);
			System.out.println("The server is waiting client input...");
			System.out.println();
			Socket socket = ss.accept();
//			ObjectOutputStream out = new ObjectOutputStream(
//					socket.getOutputStream());
			ObjectInputStream in = new ObjectInputStream(
					socket.getInputStream());
			while (true) {
				MessageEntity messageEntity = (MessageEntity) in.readObject();
				System.out.println(messageEntity.getUserName() + " "
						+ new Date().toString() +":");
				System.out.println(messageEntity.getMessage());
				System.out.println();
				if (messageEntity.getMessage().equalsIgnoreCase("quit")
						|| messageEntity.getMessage().equalsIgnoreCase("exit")) {
					in.close();
					//out.close();
					socket.close();
					break;
				}
				// print message into client socket
				//out.println("Message From Server,your input is:" + line);
				//out.flush();
			}
			ss.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new Server2();
	}

}
